package com.tanklab.signature.controller;

import com.tanklab.signature.common.util.Constant;
import com.tanklab.signature.common.util.FileUtil;
import com.tanklab.signature.common.util.sm2Util.KeyUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/signature/sm2")
public class Sm2Controller {

    @ApiOperation(value = "getPublicKey方法", notes  = "向用户前端发送公钥")
    @GetMapping("/getPublicKeyUser")
    public String getPublicKeyUser() throws Exception {
        String publicKey = null;
        String privateKey = null;
        String base64PublicKey = null;
        try {
            Map<String, String> keys = KeyUtils.generateSmKey();
            publicKey = keys.get(KeyUtils.PUBLIC_KEY);//hex公钥，需返回给前端
            privateKey = keys.get(KeyUtils.PRIVATE_KEY);//私钥，需保存
            base64PublicKey = keys.get(KeyUtils.BASE_PUBLIC_KEY);//base64公钥，需保存
            String keyPair = publicKey + "\t" + privateKey+"\t"+base64PublicKey;//本地钥对
            if(keyPair == null) {System.out.println("Error keyPair!");}
            FileUtil.writeByLine(Constant.userKeyPairsTxt,keyPair);//本地保存公钥对

        }catch (Exception e) {
            e.printStackTrace();
        }

        return publicKey;//向前端发送hex公钥
    }


//    @ApiOperation(value = "getPublicKey方法", notes  = "向商家前端发送公钥")
//    @GetMapping("/getPublicKeyBusiness")
//    public String getPublicKeyBusiness() throws Exception {
//        String publicKey = null;
//        String privateKey = null;
//        String base64PublicKey = null;
//        try {
//            Map<String, String> keys = KeyUtils.generateSmKey();
////            Map<String, String> keys = hoolTool.createKey();
//            publicKey = keys.get(KeyUtils.PUBLIC_KEY);//hex公钥，需返回给前端
//            privateKey = keys.get(KeyUtils.PRIVATE_KEY);//私钥，需保存
//            base64PublicKey = keys.get(KeyUtils.BASE_PUBLIC_KEY);//base64公钥，需保存
//            String keyPair = publicKey + "\t" + privateKey+"\t"+base64PublicKey;//本地钥对
//            if(keyPair == null) {System.out.println("Error keyPair!");}
//            FileUtil.writeByLine("src\\elbboot\\src\\main\\java\\com\\neusoft\\elmboot\\config\\businessKeyPairs.txt",keyPair);//本地保存公钥对
//
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return publicKey;//向前端发送hex公钥
//    }
}
